import {ShareDefine } from "./common/lzbjpk_ShareDefine";
import { app } from "./lzbjpk_app";
const {ccclass, property} = cc._decorator;


@ccclass
export default class bjpk_ClientManager extends cc.Component {

    @property(Node)
    globalEffect: Node = null;

    @property(Node)
    reConnectTip: Node = null;

    private JS_Name: string;
    private logSecond: number = 0;
    private isWriteLog: boolean = cc.sys.isNative;
    private clientState: number;
    private nextTick: number = 0;
    private lastHearTime: number = 0;
    private LostHearTime: number = 0;
    private isFaXinTiao: boolean = false;
    private isFirstLogin: boolean = true;
    private debugModel: any;
    private lastReConnectTime: number = 0;
    private reConnectUseTime: number = 0;
    private bStartReConnect: boolean = false;
    private reConnectLable: cc.Label;
    private startUpInfo: { [key: string]: string } = {};
    private clientConfig: { [key: string]: string } = {};
    private gameType: number = -1;

    onLoad () {
        console.log(app.subGameName + " ----- ClientManager onLoad-----");
		this.JS_Name = app.subGameName + "_ClientManager";

        this.logSecond = 0;
		//是否需要写log
		this.isWriteLog = cc.sys.isNative;

		//保证客户端管理器不被释放
		cc.game.addPersistRootNode(this.node);

		this.StartTimer();

		this.schedule(this.OnTimer, 0.1);

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.OnKeyDown, this);

        //把Client存放到App上
        app.addClient(this);

        //枚举管理器
        app.addCom('_ShareDefine',ShareDefine);
        this[app.subGameName + "_ShareDefine"] = ShareDefine;

    }

    start () {

    }

    private StartTimer () {
		this.schedule(this.OnTimer, 1);
	}
	private StopTimer () {
		this.unschedule(this.OnTimer);
	}

    	//键盘按下
	private OnKeyDown (event) {
		let keyCode = event.keyCode;
		switch (keyCode) {
			case cc.macro.KEY.back: {
				let msgID = "MSG_EXIT_GAME";
				let ConfirmManager = app[app.subGameName + "_ConfirmManager"]();
				ConfirmManager.SetWaitForConfirmForm(this.OnConFirm.bind(this), msgID, []);
				ConfirmManager.ShowConfirm(this[app.subGameName + "_ShareDefine"].Confirm, msgID, []);
				break;
			}
		}
		this.OnEvent("OnKeyDown", {"KeyCode": keyCode});
	}

    
	//100毫秒回掉
	private OnTimer (passSecond: number) {
		try {
			if (this.isWriteLog) {
				this.logSecond += passSecond;
				//500毫秒输出一次log
				if (this.logSecond >= 0.5) {
					// this.LogManager.OutPutLog();
					this.logSecond = 0
				}
			}
			if (this.clientState < this[app.subGameName + "_ShareDefine"].State_WaitLogin) {
				return
			}

			app[app.subGameName + "_SceneManager"]().OnTimer(passSecond);

			//完整登录才能调用
			if (this.clientState == this[app.subGameName + "_ShareDefine"].State_Logined) {
				let curTime = new Date().getTime();

				if (this.lastHearTime && curTime > this.lastHearTime + 23000) {
					console.error('NetWork Disconnect By Heart');
					this.lastHearTime = 0;
					if (app[app.subGameName + "_NetWork"]().isConnectIng)//ios的可能websoket不会主动断开
						app[app.subGameName + "_NetManager"]().Disconnect();

					if (!this.bStartReConnect) {
						this.StartReConnect();
						app[app.subGameName + "_NetWork"]().ReConnectByTipSureBtn();
					}
				}
				if (this.bStartReConnect) {
					if (this.lastReConnectTime && curTime > this.lastReConnectTime + 1000) {
						this.lastReConnectTime = curTime;
						this.reConnectUseTime++;
						this.reConnectLable.string = '连接失败,重新加载中(' + this.reConnectUseTime + '秒)' + '...';
					}
				}
			}
		}
		catch (error) {
			console.error("OnTimer:%s", error.stack);
		}
	}


    // update (dt) {}
}
